package com.canaan.business.service.bill.impl;

import java.util.Date;
import java.util.List;

import com.canaan.business.domain.vo.ModulationOutBillForPrintVo;
import com.canaan.common.utils.DateUtils;
import com.canaan.common.utils.SecurityUtils;
import com.canaan.common.utils.StringUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.canaan.business.mapper.bill.ModulationOutBillMapper;
import com.canaan.business.domain.bill.ModulationOutBill;
import com.canaan.business.service.bill.IModulationOutBillService;
/**
 * 出库调整单Service业务层处理
 *
 * @author canaan
 * @date 2025-03-24
 */
@Service
public class ModulationOutBillServiceImpl implements IModulationOutBillService {
    @Resource
    private ModulationOutBillMapper modulationOutBillMapper;

    /**
     * 查询出库调整单
     *
     * @param id 出库调整单主键
     * @return 出库调整单
     */
    @Override
    public ModulationOutBill selectModulationOutBillById(Long id) {
        return modulationOutBillMapper.selectModulationOutBillById(id);
    }

    /**
     * 查询出库调整单列表
     *
     * @param modulationOutBill 出库调整单
     * @return 出库调整单
     */
    @Override
    public List<ModulationOutBill> selectModulationOutBillList(ModulationOutBill modulationOutBill) {
        return modulationOutBillMapper.selectModulationOutBillList(modulationOutBill);
    }

    /**
     * 新增出库调整单
     *
     * @param modulationOutBill 出库调整单
     * @return 结果
     */
    @Override
    public int insertModulationOutBill(ModulationOutBill modulationOutBill) {
        if(StringUtils.isEmpty(modulationOutBill.getCreateBy())){
            modulationOutBill.setCreateBy(SecurityUtils.getUsername());
        }
        modulationOutBill.setCreateTime(DateUtils.getNowDate());
        return modulationOutBillMapper.insertModulationOutBill(modulationOutBill);
    }

    /**
     * 批量新增出库调整单
     *
     * @param modulationOutBillList 出库调整单列表
     * @return 结果
     */
    @Override
    public int batchInsertModulationOutBill(List<ModulationOutBill> modulationOutBillList) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (ModulationOutBill modulationOutBill : modulationOutBillList){
            modulationOutBill.setCreateBy(username);
            modulationOutBill.setCreateTime(currerTime);
        }
        int insertNum = 0;
        for (int i=0; i<modulationOutBillList.size();) {
            int endIndex = i+500;
            if (endIndex > modulationOutBillList.size()) {
                endIndex = modulationOutBillList.size();
            }
            insertNum = insertNum + modulationOutBillMapper.batchInsertModulationOutBill(modulationOutBillList.subList(i, endIndex));
            i = endIndex;
        }
        return insertNum;
    }

    /**
     * 修改出库调整单
     *
     * @param modulationOutBill 出库调整单
     * @return 结果
     */
    @Override
    public int updateModulationOutBill(ModulationOutBill modulationOutBill) {
        modulationOutBill.setUpdateBy(SecurityUtils.getUsername());
        modulationOutBill.setUpdateTime(DateUtils.getNowDate());
        return modulationOutBillMapper.updateModulationOutBill(modulationOutBill);
    }

    /**
     * 批量修改出库调整单
     *
     * @param modulationOutBillList 出库调整单列表
     * @return 结果
     */
    @Override
    public int batchUpdateModulationOutBill(List<ModulationOutBill> modulationOutBillList) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (ModulationOutBill modulationOutBill : modulationOutBillList){
            modulationOutBill.setUpdateBy(username);
            modulationOutBill.setUpdateTime(currerTime);
        }
        int updateNum = 0;
        for (int i=0; i<modulationOutBillList.size();) {
            int endIndex = i+500;
            if (endIndex > modulationOutBillList.size()) {
                endIndex = modulationOutBillList.size();
            }
            updateNum = updateNum + modulationOutBillMapper.batchUpdateModulationOutBill(modulationOutBillList.subList(i, endIndex));
            i = endIndex;
        }
        return updateNum;
    }

    /**
     * 批量删除出库调整单
     *
     * @param ids 需要删除的出库调整单主键集合
     * @return 结果
     */
    @Override
    public int deleteModulationOutBillByIds(Long[] ids) {
        return modulationOutBillMapper.deleteModulationOutBillByIds(ids);
    }

    /**
     * 删除出库调整单信息
     *
     * @param id 出库调整单主键
     * @return 结果
     */
    @Override
    public int deleteModulationOutBillById(Long id) {
        return modulationOutBillMapper.deleteModulationOutBillById(id);
    }

    @Override
    public ModulationOutBill selectModulationOutBillByNo(String no) {
        return modulationOutBillMapper.selectModulationOutBillByNo(no);
    }

    @Override
    public ModulationOutBillForPrintVo selectModulationOutBillForPrint(String no) {
        return modulationOutBillMapper.selectModulationOutBillForPrint(no);
    }
}
